﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoftwareConsulting.BI11.Studio.Core.Project;
using SoftwareConsulting.BI11.Studio.Core.Project.Metadata;
using SoftwareConsulting.BI11.Studio.Core.Project.Metadata.Extensions;
using SoftwareConsulting.BI11.Studio.Core.Project.SituationsHierarchy;
using SoftwareConsulting.BI11.Studio.UI.ProjectExplorer.Dimensions;
using SoftwareConsulting.BI11.Studio.UI.ProjectExplorer.Extensions;
using SoftwareConsulting.BI11.Studio.InternalServices;
using SoftwareConsulting.BI11.Studio.UI.Properties;
using SoftwareConsulting.BI11.Controls.Metadata;

namespace SoftwareConsulting.BI11.Studio.UI.ProjectExplorer
{
    public partial class SelectObjectDialog : RestorableForm
    {
        private MetadataObjectKind _kind;
        private ProjectExplorerForm _projectExplorerForm;
        private bool _multi;
        private ICollection<Guid> _selectedObjectsIds;

        public ICollection<Guid> SelectedObjectIds
        {
            get
            {
                return _selectedObjectsIds;
            }
        }

        /// <summary>
        /// Возвращает текущий проект
        /// </summary>
        private ProjectDescriptor ProjectLink
        {
            get
            {
                return _projectExplorerForm.ProjectLink;
            }
        }

        public SelectObjectDialog(ProjectExplorerForm projectExplorerForm, MetadataObjectKind objectKind, bool multiselectEnabled)
        {
            _multi = multiselectEnabled;
            _kind = objectKind;
            _projectExplorerForm = projectExplorerForm;            
            InitializeComponent();            
        }

        /// <summary>
        /// Применяет диалог и закрывает его
        /// </summary>
        private void ApplyAndClose()
        {            
            foreach (ListViewItem item in ObjectsBox.SelectedItems)
            {
                _selectedObjectsIds.Add(((MetadataObjectDescriptor)item.Tag).Id);
            }
            DialogResult = DialogResult.OK;
            Close();
        }

        /// <summary>
        /// Инициализирует диалог
        /// </summary>
        private void Initialize()
        {
            _selectedObjectsIds = new List<Guid>();
            string objtype = _kind.ToUserString();
            Icon ic = _kind.GetIcon();
            this.SetObjectViewerCaption(Text, objtype);            
            this.Icon = MetadataObjectKind.None.GetIcon();
            Icon = ic;
            ObjectsBox.MultiSelect = _multi;            
            foreach(MetadataObjectDescriptor info in ProjectLink.RepositoryLink.GetObjectsDescriptors(_kind))
            {
                ListView.ListViewItemCollection items = ObjectsBox.Items;                
                ListViewItem item = items.Add(info.Name);
                item.Tag = info;
                item.SubItems.Add(info.Label);
                item.SubItems.Add(info.Description);
            }
        }

        private void SelectObjectDialog_Load(object sender, EventArgs e)
        {
            Initialize();
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            ApplyAndClose();
        }

        private void ObjectsBox_DoubleClick(object sender, EventArgs e)
        {
            ApplyAndClose();
        }
    }
}
